
package com.eyedog.video.adv;

import android.text.TextUtils;
import android.util.Log;

import com.eyedog.video.entity.ADVEntity;
import com.eyedog.video.http.ICallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告位置(1启动画面，2首页顶部，3列表顶部，4搜索顶部，5搜索底部，6播放顶部，7播放底部，8游戏栏目跳转链接，9弹出公告)
 */
public class AdvInstance {

    private final String TAG = "AdvInstance";

    enum Instance {
        INSTANCE;

        AdvInstance instance;

        Instance() {
            instance = new AdvInstance();
        }

        public AdvInstance getInstance() {
            return instance;
        }
    }

    public static AdvInstance getInstance() {
        return Instance.INSTANCE.getInstance();
    }

    private AdvModel advModel;

    private boolean hasAdvInited = false, isAdvIniting = false;

    private List<ICallback<Boolean>> advCallbacks = new ArrayList<>();

    private List<ADVEntity> advEntities = new ArrayList<>();

    private AdvInstance() {
        advModel = new AdvModel();
        hasAdvInited = false;
        isAdvIniting = false;
        initAdvs();
    }

    private void initAdvs() {
        isAdvIniting = true;
        Log.i(TAG, "initAdvs " + isAdvIniting);
        advModel.loadAdvList(new ICallback<List<ADVEntity>>() {
            @Override
            public void onLoadSuccess(List<ADVEntity> advEntities) {
                isAdvIniting = false;
                hasAdvInited = true;
                Log.i(TAG, "initAdvs onLoadSuccess ");
                getInstance().advEntities = advEntities;
                for (ICallback<Boolean> callback : advCallbacks) {
                    callback.onLoadSuccess(true);
                }
                advCallbacks.clear();
            }

            @Override
            public void onLoadFailed(String err) {
                isAdvIniting = false;
                hasAdvInited = false;
                Log.i(TAG, "initAdvs onLoadFailed " + err);
                for (ICallback<Boolean> callback : advCallbacks) {
                    callback.onLoadFailed(err);
                }
                advCallbacks.clear();
            }
        });
    }

    public boolean hasAdvInited() {
        return hasAdvInited;
    }

    public void initAdv(ICallback<Boolean> callback) {
        Log.i(TAG, "initAdv callback " + isAdvIniting);
        if (isAdvIniting) {
            advCallbacks.add(callback);
            return;
        }
        initAdvs();
    }

    public ADVEntity getSplashAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("1", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getRecommendAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("2", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getVideoListAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("3", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    // public ADVEntity getCategoryAdv0(int categoryId) {
    // if (!hasAdvInited) {
    // return null;
    // }
    // ADVEntity advEntity = null;
    // return advEntity;
    // }
    //
    // public ADVEntity getCategoryAdv1(int categoryId) {
    // if (!hasAdvInited) {
    // return null;
    // }
    // ADVEntity advEntity = null;
    // return advEntity;
    // }

    public ADVEntity getNotifyAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("9", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getGameAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("8", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getSearchHeaderAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("4", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getSearchTailAdv() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("5", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getVideoDetailAdv0() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("6", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }

    public ADVEntity getVideoDetailAdv1() {
        if (!hasAdvInited) {
            return null;
        }
        ADVEntity advEntity = null;
        for (ADVEntity adv : advEntities) {
            if (TextUtils.equals("7", adv.id)) {
                advEntity = adv;
                break;
            }
        }
        return advEntity;
    }
}
